<template>
	<view class="page" :style="{'min-height':h+'px','padding-top':mt+'px'}">
		<c-nav-bar title="智慧停车"></c-nav-bar>
		<view class="top" :style="{'background':'url('+topBgImg+')','background-size':'cover'}">
			<view class="text">欢迎使用嵊泗智慧停车</view>
		</view>
		<view class="bind" v-if="!licence">
			<view class="b_box" @tap="binding">
				<image src="https://i.ringzle.com/file/20231103/73a1632fbcb84e1eb4249ee9b6796873.png"></image>
				<text>绑定车牌</text>
			</view>
		</view>
		<view class="licence" v-else>
			<view class="tc_no">{{licence}}</view>
			<view class="tc_price">{{payMoney}}<span>元</span></view>
			<view class="tc_time">
				<view>停车时长：{{parkTime}}</view>
				<view @tap="toPayMoney(licence)">立即缴费</view>
			</view>
		</view>
		<view class="menus">
			<view v-for="(item,index) in menus" :key="index" @tap="toPath(item)">
				<image :src="item.icon"></image>
				<text>{{item.name}}</text>
			</view>
		</view>
		<view class="v-bottom">
			<text>附近{{parkNum}}个停车场</text>
			<view class="v-map">
				<map id="parkmap" style="width: 100%; height: 600rpx;" :latitude="latitude" :longitude="longitude"
					:markers="markers"></map>
			</view>
		</view>
		<GetPhoneNumber ref="phoneNumber" @loginSuccess="loginSuccess"></GetPhoneNumber>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	var QQMapWX = require('@/utils/qqmap-wx-jssdk.min.js');
	var qqmapsdk = new QQMapWX({
		key: 'N5BBZ-RMQEW-AIERS-RMTYZ-HC7UT-G7FLR'
	});
	import GetPhoneNumber from '@/compoments/Getphonenumber/index.vue'
	export default {
		components: {
			GetPhoneNumber
		},
		data() {
			return {
				h: uni.getSystemInfoSync().windowHeight,
				mt: uni.getSystemInfoSync().statusBarHeight + 44,
				menus: [
					// {
					// 	name: '停车场预约',
					// 	icon: 'https://i.ringzle.com/file/20231101/97c2e776a4ff44719bb74311ee85e31c.png',
					// 	path:'/pagesIndex/parkingService/makeAnAppointment',
					// 	needLogin:false
					// },
					{
						name: '反向寻车',
						icon: 'https://i.ringzle.com/file/20231101/eb65bb02c8ff4604a0bbfc1520e3f31d.png',
						path: '/pagesIndex/parkingService/reverseSearchCar',
						needLogin: false
					},
					{
						name: '停车导航',
						icon: 'https://i.ringzle.com/file/20231101/54b6d982a7be443ba4b600ff3ee2feb8.png',
						path: '/pagesIndex/parkingService/parkingNavigation',
						needLogin: false
					},
					{
						name: '停车缴费',
						icon: 'https://i.ringzle.com/file/20231101/60b8a705ced041929014e642f515ad13.png',
						path: '/pagesIndex/parkingService/parkingFee',
						needLogin: false
					},
					{
						name: '收费标准',
						icon: 'https://i.ringzle.com/file/20231101/04a423dd1d7a4bc095d0dfb4cf5d5e1f.png',
						path: '/pagesIndex/parkingService/chargingStandard',
						needLogin: false
					},
					{
						name: '停车记录',
						icon: 'https://i.ringzle.com/file/20231101/ce5db04f7119455eb81f042bf0d350fc.png',
						path: '/pagesIndex/parkingService/parkingRecord',
						needLogin: false
					},
					{
						name: '开发票',
						icon: 'https://i.ringzle.com/file/20231101/51daa37a10284f0e8cd1fab0a63e0d31.png',
						path: '/pagesIndex/parkingService/invoice',
						needLogin: true
					},
					{
						name: '我的预约',
						icon: 'https://i.ringzle.com/file/20231101/824621437df04ddab6791d92c7625487.png',
						path: '/pagesIndex/parkingService/myAppointment',
						needLogin: true
					}
				],
				licence: '',
				parkNum: '',
				markers: [],
				longitude: '',
				latitude: '',
				payMoney: 0,
				parkTime: '',
				topBgImg: 'https://i.ringzle.com/file/20231101/a75f99aed3544061941b8308561c9852.png'

			}
		},
		onShow() {
			this.getLocation();
			this.init();
			this.getLbList()
		},
		methods: {
			getLbList() {
				this.$api.get('/sys/isappletpic/queryBannerByType/27').then(res => {
					if (res.data.code === 0) {
						let list = res.data.data.bannerList.map(b => b.path)
						if (list.length > 0) this.topBgImg = list[0];
					}
				})
			},
			init() {
				this.$login().then(res => {
					if (res === 0) {
						this.getLicence();
					} else {
						this.$refs['phoneNumber'].show = true;
					}
				})
			},
			loginSuccess() {
				this.getLicence();
			},
			getDetails(licence) {
				if (!licence) return
				this.$api.get('/travel/wisdom/ParkinPayment/whole?numberplate=' + licence + '&openId=' + JSON.parse(uni
					.getStorageSync('userInfo')).openId).then(res => {
					if (res.data.code === 0) {
						this.payMoney = parseFloat(res.data.data.paymentAmount).toFixed(2);
						let diff = Date.parse(new Date(res.data.data.paymentTime)) - Date.parse(new Date(res.data
							.data.createOrderTime));
						let diffM = Math.ceil(diff / 1000 / 60);
						let h = Math.floor(diffM / 60);
						let m = Math.ceil(diffM % 60);
						this.parkTime = h + '小时' + m + '分钟';
					} else {
						this.payMoney = '0.00';
						this.parkTime = '暂无';
						this.$showModal(res.data.msg);
					}
				})
			},
			getLicence() {
				this.$api.get('/travel/wisdom/BindingNumberplate/find', {
					openId: JSON.parse(uni.getStorageSync('userInfo')).openId
				}).then(res => {
					if (res.data.code === 0) {
						let temp = null;
						let d = res.data.data.filter(d => d.type === 1);
						if (d.length > 0) temp = d[0];
						else temp = res.data.data.length > 0 ? res.data.data[0] : null

						if (temp) {
							this.licence = temp.numberplate;
							this.getDetails(this.licence);
						}
					}
				})
			},
			getLocation() {
				let that = this;
				uni.getLocation({
					type: 'gcj02',
					success(result) {
						that.longitude = result.longitude;
						that.latitude = result.latitude;
						qqmapsdk.search({
							keyword: '停车场',
							location: result.latitude + ',' + result.longitude,
							success: function(res) {
								that.parkNum = res.data.length;
								for (var i = 0; i < res.data.length; i++) {
									that.markers.push({
										title: res.data[i].title,
										id: res.data[i].id,
										latitude: res.data[i].location.lat,
										longitude: res.data[i].location.lng,
										iconPath: "https://fsy.shengsi.gov.cn/file/20240906/f69da85565434e51849827d256efcadd.png",
										width: 20,
										height: 30
									})
								}
							}
						});
					}
				})
			},
			// 绑定车牌
			binding() {
				uni.navigateTo({
					url: '/pagesIndex/parkingService/bindLicencePlate',
					events: {
						addLicenceSuccess: data => {
							this.licence = data.licence;
						}
					}
				})
			},
			toPath(item) {
				if (['停车缴费'].includes(item.name) && !this.licence) return this.$showToast('请先绑定车牌');

				if (['开发票', '我的预约', '停车记录'].includes(item.name)) {
					return this.$login().then(res => {
						if (res === 0) {
							uni.navigateTo({
								url: item.path + '?licence=' + this.licence
							})
						} else {
							this.$refs['phoneNumber'].show = true;
						}
					})
				}

				uni.navigateTo({
					url: ['停车缴费', '停车记录', '开发票'].includes(item.name) ? (item.path + '?licence=' + this.licence) :
						item.path
				})
			},
			toPayMoney(licence) {
				uni.navigateTo({
					url: '/pagesIndex/parkingService/parkingFee?licence=' + licence
				})
			}
		}
	}
</script>

<style lang="less">
	.page {
		background: #F5F8FA;
		padding-bottom: 20rpx;

		.top {
			width: 100%;
			height: 368rpx;
			// background-image: url(https://i.ringzle.com/file/20231101/a75f99aed3544061941b8308561c9852.png);
			background-size: 100% 100%;

			.text {
				width: 100%;
				font-size: 44rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				letter-spacing: 6rpx;
				text-align: center;
				padding-top: 120rpx;
			}
		}

		.bind {
			width: calc(100% - 48rpx);
			height: 184rpx;
			margin: -94rpx 24rpx 20rpx;
			background-image: url('https://i.ringzle.com/file/20231101/31b5b953b8df454c817678bab9393c84.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.b_box {
				width: calc(100% - 216rpx);
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background: #00BBFC;
				box-shadow: 0rpx 2rpx 4rpx rgba(0, 68, 92, 0.3);
				border-radius: 46rpx;

				image {
					width: 34rpx;
					height: 34rpx;
				}

				text {
					font-size: 34rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					margin-left: 15rpx;
				}
			}
		}

		.licence {
			width: calc(100% - 96rpx);
			margin: -94rpx 24rpx 20rpx;
			background: #fff;
			border-radius: 20rpx;
			padding: 74rpx 24rpx 40rpx;

			.tc_no {
				width: 100%;
				font-size: 42rpx;
				font-family: PingFang SC;
				font-weight: bold;
				line-height: 0rpx;
				color: #007A69;
				letter-spacing: 2px;
				text-align: center;
			}

			.tc_price {
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 46rpx;
				font-family: PingFang SC;
				font-weight: 800;
				color: #FF4141;
				margin-top: 40rpx;

				span {
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					line-height: 0rpx;
					color: #111111;
					margin-left: 10rpx;
				}
			}

			.tc_time {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 60rpx;

				&>view {
					&:first-child {
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						line-height: 0rpx;
						color: #999999;
					}

					&:last-child {
						font-size: 26rpx;
						font-family: PingFang SC;
						font-weight: 400;
						line-height: 0rpx;
						color: #007A69;
					}
				}
			}
		}

		.menus {
			width: calc(100% - 48rpx);
			margin: 0 24rpx;
			border-radius: 20rpx;
			background: #fff;
			display: flex;
			flex-wrap: wrap;
			padding: 40rpx 0;

			&>view {
				width: 25%;
				display: flex;
				flex-direction: column;
				align-items: center;

				&:nth-child(1),
				&:nth-child(2),
				&:nth-child(3),
				&:nth-child(4) {
					margin-bottom: 48rpx;
				}

				image {
					width: 80rpx;
					height: 80rpx;
				}

				text {
					font-size: 28rpx;
					font-family: Source Han Sans CN;
					font-weight: 400;
					line-height: 24rpx;
					color: #333333;
					margin-top: 16rpx;
				}
			}
		}

		.v-bottom {
			width: calc(100% - 96rpx);
			padding: 20rpx 24rpx;
			background-color: #fff;
			border-radius: 10rpx;
			margin: 20rpx 24rpx;

			text {
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 0rpx;
				color: #111111;
			}

			.v-map {
				margin-top: 20rpx;
				border-radius: 20rpx;
				background: #999;
				width: 100%;
			}
		}
	}
</style>